<?php
class HotkeySaveEditAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('hotkey','edit');
	}
	
	public function execute()
	{
		$id				= $this->context->post('id');
		$cid			= $this->context->post('category_id');
		if(empty($cid))
		{
			$cid		= 0;
		}
		$name			= $this->context->post('name');
		$orderby		= $this->context->post('orderby');
		$hotkeyService	= new HotkeyService();
		if(is_numeric($id) && $id)
		{
			$hotkeyService->update($id,array('category_id'=>$cid,'name'=>$name,'orderby'=>$orderby));
			$this->code	= 200;
			$this->data['forward'] = C('LtUrl')->generate('Hotkey', 'Index',array('cid'=>$cid));
		}
		else
		{
			$name	= str_replace("\r","\n",$name);
			$names	= explode("\n",$name);
			foreach($names as $name)
			{
				if(!empty($name))
				{
					$tmp	= $hotkeyService->getList(array('category_id'=>$cid,'name'=>$name),'',0,1);
					if(empty($tmp['rows']))
					{
						$hotkeyService->add(array('category_id'=>$cid,'name'=>$name,'orderby'=>$orderby));
					}
				}
			}
			$this->code	= 200;
			$this->data['forward'] = C('LtUrl')->generate('Hotkey', 'Index',array('cid'=>$cid));
		}
	}
}
